package co.edu.usbcali.egrapico.vista.tools;

import co.edu.usbcali.egrapico.vista.FrmPrincipal;

public class ObjetoFrame extends javax.swing.JInternalFrame implements java.io.Serializable {

    private javax.swing.JLabel label;
    private javax.swing.JTextField TextField;
    private javax.swing.JList jList1;
    private javax.swing.DefaultListModel datalist;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JButton ok;
    private javax.swing.JButton cancel;
    private boolean banObject;
    private javax.swing.JPopupMenu poput;
    private javax.swing.JMenuItem item1;
    private javax.swing.JMenuItem iten2;
    int contadorM = 0;
    boolean creado;

    /**
     * Creates a new instance of ObjetoFrame
     */
    public ObjetoFrame() {
        super("",
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        banObject = false;
        creado = false;
        //******************************poput inicialices
        poput = new javax.swing.JPopupMenu();
        item1 = new javax.swing.JMenuItem();
        iten2 = new javax.swing.JMenuItem();
        item1.setText("Add method");
        item1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item1ActionPerformed(evt);
            }
        });
        poput.add(item1);
        iten2.setText("Remove Method");
        iten2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iten2ActionPerformed(evt);
            }
        });
        poput.add(iten2);

        //******************************
        datalist = new javax.swing.DefaultListModel();
        label = new javax.swing.JLabel();
        TextField = new javax.swing.JTextField();
        jList1 = new javax.swing.JList(datalist);
        scrollPane = new javax.swing.JScrollPane();
        scrollPane.setAutoscrolls(true);
        scrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("List of Methods"));
        jList1.setComponentPopupMenu(poput);
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setCellRenderer(new MyRenderList());

        //eventos....
        scrollPane.setViewportView(jList1);
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        getContentPane().setLayout(new java.awt.FlowLayout());
        setFrameIcon(null);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        label.setText("Name   ");
        getContentPane().add(label);
        TextField.setPreferredSize(new java.awt.Dimension(200, 19));
        getContentPane().add(TextField);
        getContentPane().add(scrollPane);
        scrollPane.setPreferredSize(new java.awt.Dimension(280, 280));
        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        getContentPane().add(ok);
        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        getContentPane().add(cancel);
        setBounds(60, 20, 300, 385);
    }

    public void loadProperties(ObjetoFrame ob) {
        TextField.setText(ob.TextField.getText());
        for (int i = 0; i < ob.getComponentPanelcount(); i++) {
            MethodList lab = new MethodList(TextField.getText() + "." + ob.getMethod(i).getText());
            lab.setText(ob.getMethod(i).getText());
            lab.setIcon(ob.getMethod(i).getIcon());
            lab.setHorizontalTextPosition(javax.swing.JLabel.RIGHT);
            lab.setPreferredSize(new java.awt.Dimension(240, 26));
            lab.setNamePadre(ob.getMethod(i).getNamePadre());
            lab.getFrame().loadPanelMethod(ob.getMethod(i).getFrame());
            contadorM++;
            datalist.addElement(lab);
        }
    }

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        setVisible(false);
    }

    private void okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

        if (TextField.getText().length() > 0) {
            if (jList1.getComponentCount() == 0) {
                javax.swing.JOptionPane.showMessageDialog(this, "You going to create Object without methods", "Warning Method", javax.swing.JOptionPane.WARNING_MESSAGE);
            }

            if (creado == false) {
                creado = true;
                setVisible(false);
                if (!FrmPrincipal.esObjeto(TextField.getText())) {
                    FrmPrincipal.Addobj(TextField.getText());
                } else {
                    javax.swing.JOptionPane.showMessageDialog(this, "This Object already exist", "Warning Object", javax.swing.JOptionPane.WARNING_MESSAGE);
                }
            } else {
                setVisible(false);
                FrmPrincipal.AptualizarObjeto();
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(this, "Set Name for this Object", "Error Name", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }

    public boolean iscreateObject() {
        return creado;
    }

    private void item1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        String s = javax.swing.JOptionPane.showInputDialog(this, "Write the method Name..", "Method Name", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        if (s == null || s.equals("")) {
            javax.swing.JOptionPane.showMessageDialog(this, "Set Name for the Method", "Error Name", javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            javax.swing.ImageIcon method1 = new javax.swing.ImageIcon(getClass().getResource("../imagen/method.gif"));
            MethodList lab = new MethodList(TextField.getText() + "." + s);
            lab.setText(s);
            lab.setName(s);
            lab.setIcon(method1);
            lab.setHorizontalTextPosition(javax.swing.JLabel.RIGHT);
            lab.setPreferredSize(new java.awt.Dimension(240, 26));
            contadorM++;
            datalist.addElement(lab);
        }
    }

    private void iten2ActionPerformed(java.awt.event.ActionEvent evt) {
        if (jList1.getSelectedValue() != null) {
            datalist.removeElement(jList1.getSelectedValue());
            contadorM--;
        }
    }

    public void addMehod(MethodList lab) {
        contadorM++;
        datalist.addElement(lab);
    }

    public void setNameTextField(String i) {
        TextField.setText(i);
    }

    public boolean isCreated() {
        return banObject;
    }

    public void mostrarFrame() {
        TextField.setText("");
        setVisible(true);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void adicionarFrame() {
        FrmPrincipal.addElementtoDesKtop(this);
        setVisible(true);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        banObject = true;
    }

    public int getComponentPanelcount() {
        return datalist.getSize();
    }

    public MethodList getMethod(int n) {
        return ((MethodList) datalist.getElementAt(n));
    }

    public void uplista() {
        jList1.setCellRenderer(new MyRenderList());
        jList1.repaint();
    }
}